`include "Div16_Simple.v"

module Div16_tb;
    reg [15:0] dividend, divisor;
    wire [15:0] quotient, remainder;
    wire div_by_zero;
    
    Div16_Simple divider(dividend, divisor, quotient, remainder, div_by_zero);
    
    // 显示测试结果的任务
    task display_test;
        input [15:0] a, b;
        input [15:0] expected_q, expected_r;
        begin
            dividend = a;
            divisor = b;
            #10;
            $display("Test: %d / %d = %d remainder %d (expected: %d remainder %d) %s %s", 
                     $signed(dividend), $signed(divisor), 
                     $signed(quotient), $signed(remainder),
                     $signed(expected_q), $signed(expected_r),
                     ($signed(quotient) == $signed(expected_q)) ? "Q_PASS" : "Q_FAIL",
                     ($signed(remainder) == $signed(expected_r)) ? "R_PASS" : "R_FAIL");
            if (div_by_zero) $display("  -> DIVISION BY ZERO detected");
        end
    endtask
    
    initial begin
        $display("=== 16位有符号除法器测试 ===");
        
        // 测试用例1: 基本正数除法
        display_test(16'd15, 16'd3, 16'd5, 16'd0);    // 15 / 3 = 5 余 0
        display_test(16'd16, 16'd3, 16'd5, 16'd1);    // 16 / 3 = 5 余 1
        display_test(16'd100, 16'd7, 16'd14, 16'd2);  // 100 / 7 = 14 余 2
        
        // 测试用例2: 正数 / 负数
        display_test(16'd15, 16'hFFFD, 16'hFFFB, 16'd0);  // 15 / (-3) = -5 余 0
        display_test(16'd16, 16'hFFFD, 16'hFFFB, 16'd1);  // 16 / (-3) = -5 余 1
        
        // 测试用例3: 负数 / 正数
        display_test(16'hFFF1, 16'd3, 16'hFFFB, 16'h0000);  // (-15) / 3 = -5 余 0
        display_test(16'hFFF0, 16'd3, 16'hFFFB, 16'hFFFF);  // (-16) / 3 = -5 余 -1
        
        // 测试用例4: 负数 / 负数
        display_test(16'hFFF1, 16'hFFFD, 16'd5, 16'h0000);  // (-15) / (-3) = 5 余 0
        display_test(16'hFFF0, 16'hFFFD, 16'd5, 16'hFFFF);  // (-16) / (-3) = 5 余 -1
        
        // 测试用例5: 除以1和-1
        display_test(16'd123, 16'd1, 16'd123, 16'd0);
        display_test(16'd123, 16'hFFFF, 16'hFF85, 16'd0);   // 123 / (-1) = -123
        display_test(16'hFF85, 16'd1, 16'hFF85, 16'd0);     // (-123) / 1 = -123
        display_test(16'hFF85, 16'hFFFF, 16'd123, 16'd0);   // (-123) / (-1) = 123
        
        // 测试用例6: 被除数小于除数
        display_test(16'd3, 16'd5, 16'd0, 16'd3);     // 3 / 5 = 0 余 3
        display_test(16'd1, 16'd100, 16'd0, 16'd1);   // 1 / 100 = 0 余 1
        
        // 测试用例7: 除数为0 (除零检测)
        display_test(16'd123, 16'd0, 16'd0, 16'd123);  // 123 / 0 应该检测到除零
        display_test(16'hFF85, 16'd0, 16'd0, 16'hFF85); // (-123) / 0 应该检测到除零
        
        // 测试用例8: 边界情况
        display_test(16'h7FFF, 16'd1, 16'h7FFF, 16'd0);     // 32767 / 1 = 32767
        display_test(16'h8000, 16'd1, 16'h8000, 16'd0);     // (-32768) / 1 = -32768
        display_test(16'h8000, 16'hFFFF, 16'h8000, 16'd0);  // (-32768) / (-1) = 可能溢出
        
        // 测试用例9: 相等的数相除
        display_test(16'd100, 16'd100, 16'd1, 16'd0);
        display_test(16'hFF9C, 16'hFF9C, 16'd1, 16'd0);  // (-100) / (-100) = 1
        
        $display("=== 测试完成 ===");
        $finish;
    end
    
endmodule
